﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GTServices.Entities;
using Caliburn.Micro;
using System.Windows.Data;
using GTServices.Model;
using GTServices.WPF.Client.Interfaces;

namespace GTServices.WPF.Client.ViewModels.Entities
{
    public class LanguageViewModel :PropertyChangedBase, ISelectable ,IEntityViewModel
    {
        private Language _language;
        public Language Language { 
            get { return _language; } 
            set { 
                this._language = value;
                NotifyOfPropertyChange(() => Language);
            } 
        }
        public FileExtensionViewModel SelectedFileExtension { get; set; } 
        public ListCollectionView FileExtensions
        {
            get
            {
                ListCollectionView lcv = new ListCollectionView(ModelViewModels.Instance.FileExtensionViewModels);
                lcv.Filter += new Predicate<object>(x => this.Language.FileExtensions.Contains((x as FileExtensionViewModel).FileExtension));
                return lcv;
            }
        }
        public String Name
        {
            get { return Language.Name; }
            set
            {
                Language.Name = value;
                NotifyOfPropertyChange(() => this.Name);
            }
        }
        private bool _isSelected;
        public bool IsSelected
        {

            get { return _isSelected; }
            set
            {
                _isSelected = value;
                SelectionStateChanged(value); 
                NotifyOfPropertyChange(() => IsSelected);
            }
        }
        public Object Model
        {
            get { return Language; }
            set
            {
                if (value is Language)
                    Language = value as Language;
            }
        }

        public LanguageViewModel()
        {
           
        }
        public void RemoveFileExtension()
        {
            if (this.SelectedFileExtension != null)
            {
                this.Language.FileExtensions.Remove(this.SelectedFileExtension.FileExtension);
                NotifyOfPropertyChange(() => FileExtensions);
            }

        }
        public void AddFileExtension()
        {
            var fevms = new List<FileExtensionViewModel>();
            foreach (FileExtensionViewModel fevm in this.FileExtensions)
            {
                fevms.Add(fevm);
            }

            var fileExtensions = ModelViewModels.Instance.FileExtensionViewModels.ToList().Except(fevms);

            ListPickerViewModel lpvm = new ListPickerViewModel(fileExtensions.ToList<Object>(), "FileExtension.Extension", "FileExtension.Extension" , System.Windows.Controls.SelectionMode.Extended);

         
            if (GTWindowManager.Instance.ShowDialog(lpvm, 300, 300) == true && lpvm.SelectedItems.Count > 0)
            {
                foreach (FileExtensionViewModel fevm in lpvm.SelectedItems)
                {
                    if (!this.Language.FileExtensions.Contains(fevm.FileExtension))
                    {
                        this.Language.FileExtensions.Add(fevm.FileExtension);
                    }
                }
                NotifyOfPropertyChange(() => FileExtensions);
            }
        }
        private void SelectionStateChanged(bool value)
        {

            LanguageViewModel lvm = ModelViewModels.Instance.LanguageViewModels.FirstOrDefault(x => x.Name == "All File Types");

            if (value == true && 
                lvm != null   && 
                lvm.IsSelected && 
                lvm != this
                )
            {
                lvm.IsSelected = false;
            }
            foreach (CategoryViewModel cvm in ModelViewModels.Instance.CategoryViewModels.ToList())
            {
                if (cvm.Category.Patterns.FirstOrDefault(x => x.Language == this.Language) != null ||
                    this.Language.Name == "All File Types")
                {
                    cvm.IsSelected = value;
                }
            }
        }
    }
}
